var express = require('express');
var router = express.Router();
var mysql = require('../mysql/fun');
var createError = require('http-errors');
/**
 * 所有的api请求
 */
router.post('/login', function (req, res, next) {
  var bodys = req.body;
  var username = bodys.username;
  var password = bodys.password;
  //判断验证码
  // var sessionCaptcha = req.session.captcha.toLowerCase();
  // if (sessionCaptcha != bodys.verify) {
  //   return res.send({
  //     flog: 0,
  //     data: '验证码不正确'
  //   });
  // };
  mysql.login(username, password, function (err, data) {
    if (err) {
      next("未知错误");
    }
    if (!data.length) {
      return res.json({
        flog: 0,
        data: '输入信息有误，请重新输入'
      });
    } else {
      req.session.username = username;
      res.json({
        flog: 1,
        data: data[0]
      });
    }
  })
});
router.post('/register', function (req, res, next) {
  var bodys = req.body;
  console.log(bodys)
  mysql.isuser(bodys.name, function (err, data) {
    if (err) {
      return next(err);
    }
    if (!data.code) {
      return res.json({
        flog: 0,
        data: '服务器已经存在该用户'
      });
    } else {
      mysql.register({
        username: bodys.name,
        password: bodys.pass,
        email: bodys.email
      }, function (err, data) {
        if (err) {
          return next(err);
        }
        if (data) {
          req.session.username = bodys.name;
          return res.json({
            flog: 1,
            data: '注册用户成功'
          });
        } else {
          return res.json({
            flog: 0,
            data: '注册用户失败'
          });
        }
      })
    }
  })
});
router.post('/dellogin', function (req, res, next) {
  req.session.destroy(function (err) {
    if (err) {
      return res.json({
        flog: 0,
        data: '退出失败'
      });
    }
    res.clearCookie("user_key");
    return res.json({
      flog: 1,
      data: '退出成功'
    });
  })
});
router.get('/islogin', function (req, res, next) {
  if (req.session.username) {
    return res.json({
      flog: 1,
      data: '已经登录'
    });
  } else {
    return res.json({
      flog: 0,
      data: '未登录'
    });
  }
});
router.get('/shoppinfo', function (req, res, next) {
  var id = req.query.id;
  mysql.commoditybyid(id, function (err, data) {
    if (err) {
      return res.json({
        flog: 0,
        data: '获得信息失败'
      });
    }
    return res.json({
      flog: 1,
      data: data
    });
  });
});
router.get('/commodityall', function (req, res, next) {
  mysql.commodityall(function (err, data) {
    if (err) {
      return res.json({
        flog: 0,
        data: '获得信息失败'
      });
    }
    return res.json({
      flog: 1,
      data: data
    });
  })
})
router.get('/commoditybyname', function (req, res, next) {
  var name = req.query.name;
  mysql.commoditybyname(name, function (err, data) {
    if (err) {
      return res.json({
        flog: 0,
        data: '获得信息失败'
      });
    }
    return res.json({
      flog: 1,
      data: data
    });
  });
})
router.post('/addshopcardinfo', function (req, res, next) {
  mysql.addshopcardinfo({
    username: req.session.username,
    comm: req.body.data,
  }, function (err, data) {
    if (err) {
      return next("未知错误");
    }
    return res.json({
      flog: 1,
      data: '添加商品成功'
    });
  })
})
router.get('/delcard', function (req, res, next) {
  var id = req.query.id;
  mysql.delcardbyid(id, function (err, data) {
    if (err) {
      return next("未知错误");
    }
    return res.json({
      flog: 1,
      data: '删除订单成功'
    });
  })
})
/**
 * 处理所有api错误
 */
router.use(function (err, req, res, next) {
  console.log(err)
  return res.send({
    flog: 0,
    data: err
  })
});
module.exports = router;
